package ee.siimkaspar.subjects.service;

import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import ee.siimkaspar.subjects.model.UserAccount;

@Transactional
public class UserService extends BaseService {

	 public UserAccount getAccountByUsernameAndPassword(String username, String password) {
		 try {
			 Query q = getEntityManager().createQuery("select ua from UserAccount ua where ua.username=:username and ua.passw=:psw");
			 q.setParameter("username", username).setParameter("psw", password);
			 return (UserAccount) q.getSingleResult();
		 } catch (Exception e) {
			Logger.getLogger(UserService.class).warn(e.getMessage());
		}
		 return null;
	 }
	 
	 public UserAccount getAccountByIdAndPassword(Integer id, String password) {
		 try {
			 Query q = getEntityManager().createQuery("select ua from UserAccount ua where ua.id=:id and ua.passw=:psw");
			 q.setParameter("id", id).setParameter("psw", password);
			 return (UserAccount) q.getSingleResult();
		 } catch (Exception e) {
			 Logger.getLogger(UserService.class).warn(e.getMessage());
		 }
		 return null;
	 }
	
}
